package cn.gene.timeyan.article.webapi.service.impl;

import cn.gene.timeyan.article.service.LikeService;
import cn.gene.timeyan.article.service.Repository;
import cn.gene.timeyan.article.webapi.util.LikedStatusEnum;
import cn.gene.timeyan.article.webapi.util.RedisKeyUtils;
import cn.gene.timeyan.pojo.article.model.ArticleVO;
import cn.gene.timeyan.pojo.article.model.LikeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * <p>
 * redis
 * </p>
 *
 * @author LWG
 * @since 2022-07-25
 */
public class repository implements Repository {
   /* @Autowired
    private RedisTemplate<String, Serializable> redisTemplate;

    @Autowired
    private LikeService likeService;

    @Override
    public void saveLikedRedis(Long  likeId, Long  byUserId) {
        String key= RedisKeyUtils.getLikedKey(likeId,byUserId);
        redisTemplate.opsForHash().put(RedisKeyUtils.MAP_KEY_USER_LIKED,key, LikedStatusEnum.LIKE.getCode());
    }


    @Override
    public void unlikeFromRedis(Long  likeId, Long  byUserId) {
        String key= RedisKeyUtils.getLikedKey(likeId,byUserId);
        redisTemplate.opsForHash().put(RedisKeyUtils.MAP_KEY_USER_LIKED,key, LikedStatusEnum.UNLIKE.getCode());

    }

    @Override
    public void deleteLikedFromRedis(Long  likeId, Long  byUserId) {
        String key= RedisKeyUtils.getLikedKey(likeId,byUserId);
        redisTemplate.opsForHash().delete(RedisKeyUtils.MAP_KEY_USER_LIKED,key);
    }

    @Override
    public void incrementLikedCount(Long userId) {
        redisTemplate.opsForHash().increment(RedisKeyUtils.MAP_KEY_USER_LIKED_COUNT, userId, 1);
    }

    @Override
    public void decrementLikedCount(Long  userId) {
        redisTemplate.opsForHash().increment(RedisKeyUtils.MAP_KEY_USER_LIKED_COUNT, userId, -1);
    }

    @Override
    public List<LikeVO> getLikedDataFromRedis() {
        Cursor<Map .Entry<Object,Object>> cursor=redisTemplate.opsForHash().scan(RedisKeyUtils.MAP_KEY_USER_LIKED, ScanOptions.NONE);
        List<LikeVO> list=new ArrayList<>();
        while (cursor.hasNext()){
            Map.Entry<Object,Object> entry= cursor.next();
            String key=(String) entry.getKey();
            String[] spilt=key.split("::");
            Long likeId=Long.valueOf(spilt[0]);
            Long byUserId=Long.valueOf(spilt[1]);
            String state= (String) entry.getValue();
            LikeVO like=new LikeVO(likeId,byUserId,state);
            list.add(like);
            redisTemplate.opsForHash().delete(RedisKeyUtils.MAP_KEY_USER_LIKED,key);
        }
        return list;

    }

    @Override
    public List<ArticleVO> getLikedCountFromRedis() {
        Cursor<Map .Entry<Object,Object>> cursor=redisTemplate.opsForHash().scan(RedisKeyUtils.MAP_KEY_USER_LIKED, ScanOptions.NONE);
        List<ArticleVO> list=new ArrayList<>();
        while (cursor.hasNext()) {
            Map.Entry<Object, Object> map = cursor.next();
            //将点赞数量存储在类中
            Long key = (Long) map.getKey();
            ArticleVO vo = new ArticleVO(key, (Integer) map.getValue());
            list.add(vo);
            redisTemplate.opsForHash().delete(RedisKeyUtils.MAP_KEY_USER_LIKED_COUNT, key);
        }
        return list;
    }*/
}
